define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entrust/index' + location.search,
                    add_url: 'entrust/add',
                    edit_url: 'entrust/edit',
                    del_url: 'entrust/del',
                    multi_url: 'entrust/multi',
                    import_url: 'entrust/import',
                    table: 'entrust',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
						[
							{checkbox: true},
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'user.username', title: __('Username'), operate: 'LIKE'},
							{field: 'user.mobile', title: __('手机'), operate: 'LIKE'},
							{
								field: 'mode',
								searchable:false, 
								title: __('委托类型'), 
								searchList: {"buy":__('买入'),"sell":__('卖出')},
								formatter:Table.api.formatter.normal,
								custom:{'sell':'yellow','buy':'info'}
							},
							{field: 'num', title: __('委托数量')},
							{field: 'all_no', title: __('股票编码')},
							{field: 'name', title: __('股票名词')},
							{field: 'deal_price', title: __('委托价格')},
							{field: 'createtime', title: __('委托时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{
								field: 'is_auto', 
								title: __('自动管理'),
								formatter:Table.api.formatter.normal,
								searchList: {"1":__('是'),"0":__('否')},
							},
							{
								field: 'status',
								searchable:false, 
								title: __('Status'), 
								searchList: {"1":__('委托中'),"0":__('已取消'),"2":__('已成交')}, 
								formatter: Table.api.formatter.status,
								custom:{'0':'danger','1':'info','2':'success'}
							},
						]
					]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
			
			$('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
				
                var value = $(this).data("value");
				
                var options = table.bootstrapTable('getOptions');
					options.pageNumber = 1;
					options.queryParams = function (params) {
					  
						params.type = JSON.stringify(value);
						return params;
					};
					table.bootstrapTable('refresh', {});
					return false;
                
            });
			// 搜索
			$("form.form-commonsearch button[type='submit']").on("click", function (event) {
                   event.stopPropagation();
                   event.preventDefault();
                   var tab_search_value = $('.nav-tabs .active').data('value');
                var filter = {};
                var op = {};
                var commonsearch = $('.commonsearch-table');
                $("form.form-commonsearch .operate", commonsearch).each(function (i) {
                    var name = $(this).data("name");
                    var sym = $(this).is("select") ? $("option:selected", this).val() : $(this).val().toUpperCase();
                    var obj = $("[name='" + name + "']", commonsearch);
                    if (obj.size() == 0){
                        return true;
                    }
                    
                    var inputval = (typeof $("select[name='"+name+"']").val() != 'undefined') ? $("select[name='"+name+"']").val() : $("input[name='"+name+"']").val();
                    if ((typeof inputval == 'undefined') || inputval == '' || inputval == null || ($.isArray(inputval) && inputval.length == 0) && !sym.match(/null/i)) {
                        return true;
                    }
                    op[name] = sym;
                    filter[name] = inputval;
                });
                // console.log({op: op, filter: filter});
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var customp = {};
                    customp.tab_search = tab_search_value;
                    params.customp = JSON.stringify(customp);
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
                table.bootstrapTable('refresh');
            });

               $("form.form-commonsearch button[type='reset']").on("click", function(event){
                   event.stopPropagation();
                   event.preventDefault();
                   var tab_search_value = $('.nav-tabs .active').data('value');
                var filter = {};
                var op = {};
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var customp = {};
                    customp.tab_search = tab_search_value;
                    params.customp = JSON.stringify(customp);
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
                $("form.form-commonsearch")[0].reset();
                table.bootstrapTable('refresh');
               });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
          
    return Controller;
});
